package com.cdp.web.controller.system;

import com.cdp.common.core.domain.AjaxResult;
import com.cdp.common.core.domain.entity.SysProportioningDesignModuleHistory;
import com.cdp.common.utils.SecurityUtils;
import com.cdp.system.service.SysProportioningDesignModuleHistoryService;
import com.cdp.system.vo.ProportioningDesignModuleHistoryVO;
import com.cdp.system.vo.SysPerformancePredictedModuleHistoryDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 配比设计
 */
@RestController
@RequestMapping
@Slf4j
public class SysProportioningDesignModuleHistoryController {


    @Autowired
  private   SysProportioningDesignModuleHistoryService sysProportioningDesignModuleHistoryService;

    /**
     * 查询配比设计模版
     * @return
     */
    /**
     * 保存配比设计模块历史记录
     * @param dto 请求数据
     * @return AjaxResult
     */
    @PostMapping("/saveProportioningDesignModuleHistory")
    public AjaxResult saveProportioningDesignModuleHistory(@Validated @RequestBody SysPerformancePredictedModuleHistoryDTO dto) {
        try {
            SysProportioningDesignModuleHistory moduleHistory = new SysProportioningDesignModuleHistory();
            moduleHistory.setDesignName(dto.getName());
            moduleHistory.setFilePath(dto.getFilePath());
            moduleHistory.setCreateTime(LocalDateTime.now());
            moduleHistory.setUserId(SecurityUtils.getUserId());
            moduleHistory.setDeleteTime(0);
            moduleHistory.setVersion(0);
            moduleHistory.setExcelUrl(dto.getExcelUrl());

            boolean saved = sysProportioningDesignModuleHistoryService.save(moduleHistory);
            if (!saved) {
                log.error("保存配比设计历史失败");
                return AjaxResult.error("保存失败，数据库操作未成功");
            }

            return AjaxResult.success("保存成功");

        } catch (Exception e) {
            return AjaxResult.error("保存配比设计失败: " + e.getMessage());
        }
    }



    @PostMapping("queryProportioningDesignModuleHistory")
    public  AjaxResult queryProportioningDesignModuleHistory(@RequestBody SysPerformancePredictedModuleHistoryDTO dto){
     List<ProportioningDesignModuleHistoryVO> proportioningDesignModuleHistoryVOS = sysProportioningDesignModuleHistoryService.queryProportioningDesignModuleHistory(dto);
        return AjaxResult.success(proportioningDesignModuleHistoryVOS);
    }







}
